#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int main(int argc, const char* argv[])
{
  const char * filename = argv[1];
  const char * fileOutName = argv[2];
  const int screenSize = 32 * 32 * 2;

  FILE * fileIn = fopen(filename, "rb");
  FILE * fileOut = fopen(fileOutName, "wb");

  fseek(fileIn, 0, SEEK_END);
  long fileSize = ftell(fileIn);
  fseek(fileIn, 0, SEEK_SET);

  unsigned char * bufferIn = (unsigned char*)malloc(fileSize);
  unsigned char * bufferOut =(unsigned char*) malloc(fileSize);
  fread(bufferIn, 1, fileSize, fileIn);

  int nbScreen = fileSize/screenSize;

  //for each screen
  for(int i=0 ; i<nbScreen ; i++)
    {
      memcpy(bufferOut + fileSize - ((i+1) *screenSize), bufferIn + (i*screenSize), screenSize);
    }
  
  fclose(fileIn);
  fwrite(bufferOut, 1, fileSize, fileOut);
  fclose(fileOut);

  free(bufferIn);
  free(bufferOut);
}
